package com.market.web.controller;

import java.util.List;

import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Product;
import com.market.domain.ProductPrice;
import com.market.domain.ProductTheme;
import com.market.manager.ProductThemeManager;
import com.market.util.CollectionUtil;
import com.market.web.model.ProductModel;
import com.market.web.model.ProductThemeModel;

@Controller("productThemeController")
@Scope("session")
public class ProductThemeController extends
		GenericController<ProductThemeManager, ProductThemeModel, ProductTheme> {

	@Autowired
	private ProductModel productModel;

	@Autowired
	@Override
	protected void setModel(ProductThemeModel model) {
		this.model = model;
	}

	@Autowired
	@Override
	protected void setManager(ProductThemeManager manager) {
		this.manager = manager;
	}

	public void loadActiveProductThemes() {
		if (this.getModel().getActiveProductThemes() == null) {
			this.getModel().setActiveProductThemes(
					this.manager.fetchActiveProductThemes());
		}
	}

	/**
	 * When user select different tab of theme, this method will be invoked to
	 * load all products of correspondent theme
	 * 
	 * @param event
	 */
	public void onTabChange(TabChangeEvent event) {
		Long id = Long.parseLong(event.getTab().getId());
		/*
		 * for(ProductTheme theme : this.getModel().getList()){ if(theme.getId()
		 * == id){ theme = this.manager.fetchOne(id);
		 * this.setModelCurrent(theme); break; } }
		 */
	}

	/**
	 * Add all selected products to current product theme And clear selected
	 * propertied after this operation finish
	 * 
	 */
	public void addSelectedProductsToCurrent() {
		ProductTheme current = this.getModelCurrent();
		List<Product> selectedProducts = productModel.getSelecteds();

		List<Product> products = current.getProducts();
		Boolean exist = Boolean.FALSE;
		for (Product product : selectedProducts) {
			exist = Boolean.FALSE;
			for (Product productInTheme : products) {
				if (product.getId().equals(productInTheme.getId())) {
					exist = Boolean.TRUE;
					break;
				}
			}
			if (exist == Boolean.FALSE) {
				current.getProducts().add(product);
			}
		}
		CollectionUtil<Product> productUtil = new CollectionUtil<Product>();
		productUtil.clearSelection(selectedProducts); // Normally we have to
														// clear selected
														// properties, after
														// operation finish
	}

	/**
	 * Remove all selected products from current product theme And clear
	 * selected propertied after this operation finish
	 * 
	 */

	public void removeSelectedProductsFromCurrent() {
		List<Product> products = this.getModel().getCurrent().getProducts();
		CollectionUtil<Product> util = new CollectionUtil<Product>();
		// List<Product> unSelectedProducts = util.extractUnSelected(products);
		// this.getModelCurrent().setProducts(unSelectedProducts);

		List<Product> selectedProducts = util.extractSelected(products);
		util.removeAll(products, selectedProducts);
		if (this.getModel().getCurrent().getFilteredProducts() != null) {  //If in filtered mode
			util.removeAll(this.getModel().getCurrent().getFilteredProducts(),  // remove selected products from filtered list too.
					selectedProducts);
		}
		util.clearSelection(products); // Normally we have to clear
										// selected properties, after
										// operation finish
	}
}
